package edu.city.class_2201_sno_202212008.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import edu.city.class_2201_sno_202212008.Adapter.QuestionAdapter;
import edu.city.class_2201_sno_202212008.Domain.QuestionModel;
import edu.city.class_2201_sno_202212008.R;
import edu.city.class_2201_sno_202212008.databinding.ActivityQuestionBinding;

import java.util.ArrayList;
import java.util.List;


public class QuestionActivity extends AppCompatActivity {
    private ActivityQuestionBinding binding;
    private int position = 0;
    private ArrayList<QuestionModel> receiverList = new ArrayList<>();
    private int allscore = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityQuestionBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
        );

        receiverList = getIntent().getParcelableArrayListExtra("list");
        if (receiverList == null) receiverList = new ArrayList<>();

        binding.backBtn.setOnClickListener(v -> finish());
        binding.progressBar.setProgress(1);

        binding.questionTxt.setText(receiverList.get(position).getQuestion());
        int drawableResourceId = binding.getRoot().getResources().getIdentifier(
                receiverList.get(position).getPicPath(), "drawable", binding.getRoot().getContext().getPackageName()
        );
        if (drawableResourceId != 0) {
            Glide.with(this)
                .load(drawableResourceId)
                .centerCrop()
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(60)))
                .into(binding.pic);
        } else {
            binding.pic.setImageResource(R.drawable.ic_launcher_background);
        }

        loadAnswers();

        binding.rightArrow.setOnClickListener(v -> {
            if (binding.progressBar.getProgress() == 10) {
                Intent intent = new Intent(QuestionActivity.this, ScoreActivity.class);
                intent.putExtra("Score", allscore);
                startActivity(intent);
                finish();
                return;
            }
            position++;
            binding.progressBar.setProgress(binding.progressBar.getProgress() + 1);
            binding.questionNumberTxt.setText("Question" + binding.progressBar.getProgress() + "/10");
            binding.questionTxt.setText(receiverList.get(position).getQuestion());
            int drawableResourceId1 = binding.getRoot().getResources().getIdentifier(
                    receiverList.get(position).getPicPath(), "drawable", binding.getRoot().getContext().getPackageName()
            );
            if (drawableResourceId1 != 0) {
                Glide.with(this)
                    .load(drawableResourceId1)
                    .centerCrop()
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(60)))
                    .into(binding.pic);
            } else {
                binding.pic.setImageResource(R.drawable.ic_launcher_background);
            }
            loadAnswers();
        });

        binding.leftArrow.setOnClickListener(v -> {
            if (binding.progressBar.getProgress() == 10) {
                return;
            }
            position--;
            binding.progressBar.setProgress(binding.progressBar.getProgress() + 1);
            binding.questionNumberTxt.setText("Question" + binding.progressBar.getProgress() + "/10");
            binding.questionTxt.setText(receiverList.get(position).getQuestion());
            int drawableResourceId2 = binding.getRoot().getResources().getIdentifier(
                    receiverList.get(position).getPicPath(), "drawable", binding.getRoot().getContext().getPackageName()
            );
            if (drawableResourceId2 != 0) {
                Glide.with(this)
                    .load(drawableResourceId2)
                    .centerCrop()
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(60)))
                    .into(binding.pic);
            } else {
                binding.pic.setImageResource(R.drawable.ic_launcher_background);
            }
            loadAnswers();
        });
    }

    private void loadAnswers() {
        List<String> users = new ArrayList<>();
        users.add(String.valueOf(receiverList.get(position).getAnswer_1()));
        users.add(String.valueOf(receiverList.get(position).getAnswer_2()));
        users.add(String.valueOf(receiverList.get(position).getAnswer_3()));
        users.add(String.valueOf(receiverList.get(position).getAnswer_4()));

        QuestionAdapter questionAdapter = new QuestionAdapter(String.valueOf(receiverList.get(position).getCorrectAnswer()), users, this);
        binding.questionList.setLayoutManager(new LinearLayoutManager(this));
        binding.questionList.setAdapter(questionAdapter);
    }

    public void amount(int number, String clickedAnswer) {
        allscore += number;
        receiverList.get(position).setClickedAnswer(clickedAnswer);
    }
} 